﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.Dao;
using LinkCard.Model;
using LinkCard.Utils;

namespace LinkCard.CustomerView.CusCp
{
    public partial class transaction_report : System.Web.UI.Page
    {
        public string sFromDate = "";
        public string sToDate = "";
        public string sCardNumber = "";
        public DateTime dFromDate = new DateTime(1900, 1, 1);
        public DateTime dToDate = new DateTime(2099, 1, 1);

        private int _totalRecords;

        private int nPageSize = 40;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadPayment();
        }

        private void LoadPayment()
        {
            var db = new TopLinkEntities();

            if (Session["USERID"] == null)
                Response.Redirect("login.aspx");
            else
            {
                Int64 nCustomerID = Convert.ToInt64(Session["USERID"].ToString());

                int curpage = 0;
                int skip = 0;

                if (Request.QueryString["page"] != null)
                {
                    if (Request.QueryString["page"].ToString() != "1")
                    {
                        curpage = Convert.ToInt32(Request.QueryString["page"]);
                        skip = curpage * nPageSize;
                    }
                    else
                    {
                        curpage = 1;
                        skip = 0;
                    }
                }
                else
                {
                    curpage = 1;
                    skip = 0;
                }

                System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
                dateInfo.ShortDatePattern = "MM/dd/yyyy";

                if (sFromDate != "")
                {
                    sFromDate += " 00:00:00";
                    dFromDate = Convert.ToDateTime(sFromDate, dateInfo);

                }
                if (sToDate != "")
                {
                    sToDate += " 23:59:59";
                    dToDate = Convert.ToDateTime(sToDate, dateInfo);
                }

                var _paylog = (from payment in db.PayLog
                               from supplier in db.Supplier
                               where (supplier.SupplierID == payment.SupplierID
                                     && payment.CustomerID == nCustomerID
                                     && payment.PayDate >= dFromDate
                                     && payment.PayDate <= dToDate)
                               select new
                               {
                                   payment.CustomerID,
                                   payment.PayID,
                                   payment.PayDate,
                                   payment.PayAmount,
                                   payment.PayMethod,
                                   payment.PayPointAdd,
                                   supplier.SupplierID,
                                   supplier.Address,
                                   supplier.Name
                               }).OrderBy(t => t.PayDate).Skip(skip).Take(nPageSize);

                var paylog = _paylog.ToList();

                if (paylog.Count > 0)
                {
                    _totalRecords = paylog.Count;

                    int nPageCount;

                    if (_totalRecords % nPageSize == 0)
                    {
                        nPageCount = _totalRecords / nPageSize;
                    }
                    else
                    {
                        nPageCount = Convert.ToInt32(_totalRecords / nPageSize) + 1;
                    }

                    if (nPageCount < 2) divPager.Visible = false;

                    PagerShow.Text = LinkCard.Utils.hash1way.ShowPageNavigation(curpage, nPageCount);

                    msgList.DataSource = paylog;

                    msgList.DataBind();
                    msgNoData.Visible = false;
                }
                else
                {

                    msgList.DataSource = null;
                    msgList.DataSourceID = null;
                    msgList.DataBind();
                    msgNoData.Visible = true;
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            sFromDate = FromDate.Value.Trim();
            sToDate = ToDate.Value.Trim();
            LoadPayment();
        }
    }
}